#include "Textbox.h"
using namespace std;

Textbox::Textbox(int x, int y, int w, int h, string t, int tw)
{
    tb_x = x; tb_y = y; tb_w = w; tb_h = h;
	title = t; titleWidth = tw;
	text = ""; winTitle = "";
}

void Textbox::settitle(string t)
{
	winTitle = t;
}

void Textbox::show()
{
    setfillcolor(RGB(255, 255, 255)); //设置文本框白色
    fillroundrect(tb_x, tb_y, tb_x + tb_w, tb_y + tb_h, 5, 5);

    settextcolor(RGB(0, 0, 0));
	//设置文本大小，垂直压缩，字体
	settextstyle(32, 0, "黑体");
	//去除文字背景
	setbkmode(TRANSPARENT);
	outtextxy(tb_x, tb_y + 10, title.c_str());
	outtextxy(tb_x + titleWidth, tb_y + 10, text.c_str());
}

bool Textbox::isCliked()
{
	if (tb_msg.x >= tb_x && tb_msg.x <= tb_x + tb_w && tb_msg.y >= tb_y && tb_msg.y <= tb_y + tb_h) {
		if (tb_msg.message == WM_LBUTTONDOWN) {
			return true;
		}
	}
	return false;
}

void Textbox::updatetext(int enter) //用于输入框实时更新，enter为1则使用换行显示，为0则一行显示
{
	if (isCliked()) {
		char getin[100];
		InputBox(getin, 50, winTitle.c_str(), nullptr);
		text = getin;

		setfillcolor(RGB(255, 255, 255)); //设置文本框白色
		fillroundrect(tb_x, tb_y, tb_x + tb_w, tb_y + tb_h, 5, 5);

		settextcolor(RGB(0, 0, 0));
		//设置文本大小，垂直压缩，字体
		settextstyle(32, 0, "黑体");
		//去除文字背景
		setbkmode(TRANSPARENT);
		outtextxy(tb_x, tb_y + 10, title.c_str());

		//文本框内容显示
if (enter == 0) {
	outtextxy(tb_x + titleWidth, tb_y + 10, text.c_str());
}
else {
	string line;
	int linex = tb_x + titleWidth;
	int liney = tb_y + 10 - 38;
	for (int i1 = 0, i2 = 0; i1 <= (text.length()); i1++) {
		if (i1 % 18 == 0 && i1 != 0) { //截取出9个字符在输入框中显示为一行
			line = text.substr(i2, i1-1);
			i2 = i1;
			liney = liney + 38;  //每行宽度为38
			outtextxy(linex, liney, line.c_str());
		}
		else if (i1 == text.length()) { //最后一行不足9个字符
			line = text.substr(i2, i1);
			liney = liney + 38;  //每行宽度为38
			outtextxy(linex, liney, line.c_str());
		}
		else {

		}
	}
}
	}
}

void Textbox::setmsg(ExMessage& m)
{
	tb_msg = m;
}

void Textbox::outRemain(string r) //输入错误时输入框显示提示
{
	text = r;
	setfillcolor(RGB(255, 255, 255)); //设置文本框白色
	fillroundrect(tb_x, tb_y, tb_x + tb_w, tb_y + tb_h, 5, 5);

	settextcolor(RGB(224, 0, 0));
	//设置文本大小，垂直压缩，字体
	settextstyle(32, 0, "黑体");
	//去除文字背景
	setbkmode(TRANSPARENT);
	outtextxy(tb_x, tb_y + 10, title.c_str());
	outtextxy(tb_x + titleWidth, tb_y + 10, text.c_str());
}
